package org.jxf.gps.jt808.message;

import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.jfk.core.utils.ByteUtils;

/**
 * 终端注册
 * @author jxf
 * 2015年10月21日
 */
public class Msg0100  extends MsgBody{
	private static Logger logger = Logger.getLogger(Msg0100.class);

	
	byte[] _province = new byte[2];
	byte[] _city = new byte[2];
	byte[] _factory = new byte[5];
	byte[] _terminalType;
	byte[] _terminalId = new byte[7];
	byte[] _vechilePlateColor = new byte[1];
	byte[] _vechileId;

	@Override
	public byte[] toBuff() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void fromBuff(byte[] buff) {
		logger.debug(ByteUtils.byteToHexString(buff));
		int idx = 0;
		System.arraycopy(buff, idx , _province, 0 , _province.length);
		idx = idx + _province.length;

		System.arraycopy(buff, idx , _city, 0 , _city.length);
		idx = idx + _city.length;
		
		System.arraycopy(buff, idx , _factory, 0 , _factory.length);
		idx = idx + _factory.length;
		
		if (buff.length == 34){//2011版
			_terminalType = new byte[8];
		}else{//2013版
			_terminalType = new byte[20];
		}
		System.arraycopy(buff, idx , _terminalType, 0 , _terminalType.length);
		idx = idx + _terminalType.length;
		
		System.arraycopy(buff, idx , _terminalId, 0 , _terminalId.length);
		idx = idx + _terminalId.length;
		
		System.arraycopy(buff, idx , _vechilePlateColor, 0 , _vechilePlateColor.length);
		idx = idx + _vechilePlateColor.length;
		
		_vechileId = new byte[buff.length - idx];
		System.arraycopy(buff, idx , _vechileId, 0 , _vechileId.length);
		idx = idx + _vechileId.length;
		
		logger.debug("终端注册 :" + this);
	}
	
	public String toString(){
		String s = "";
		try {
			s = " _province = " + ByteUtils.byte2ToInt(_province) +
				" _city = " + ByteUtils.byte2ToInt(_city) +
				" _factory = " + ByteUtils.bytesToASCII(_factory) +
				" _terminalType = " + ByteUtils.bytesToASCII(_terminalType) +
				" _terminalId = " + ByteUtils.bytesToASCII(_terminalId) +
				" _vechilePlateColor = " + _vechilePlateColor[0] +
				" _vechileId = " + new String(_vechileId,"GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
				
	}

}
